﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace FrameworkQ.DataCopy
{
    public class ExcelSource: DataConnectableBase, IDataSource
    {
        private const string SCHEMA_TABLENAME = "TABLE_NAME";
        private const string SCHEMA_COLUMNNAME = "COLUMN_NAME";
        private const string SCHEMA_ORDINAL = "ORDINAL_POSITION";

        private const string DT_GENERAL = 5;
        private const string DT_GENERAL = 5;
 

        public IDictionary<string, ColumnDefinition> GetColumns()
        {
            throw new NotImplementedException();
        }

        public IList<string> GetTables()
        {
            List<string> tables = new List<string>();
            DataTable dt = _conn.GetSchema(OleDbMetaDataCollectionNames.Tables);
            foreach (DataRow row in dt.Rows)
            {
                tables.Add(row[SCHEMA_TABLENAME].ToString());
            }
            return tables;
        }

        public IList<ColumnDefinition> GetColumns(string tableName)
        {
            List<ColumnDefinition> cols = new List<ColumnDefinition>();
            DataTable dt = _conn.GetSchema(OleDbMetaDataCollectionNames.Columns);
            foreach (DataRow row in dt.Rows)
            {
                string table = row[SCHEMA_TABLENAME].ToString();
                if ( table == tableName)
                {
                    ColumnDefinition col = new ColumnDefinition();
                    col.Name = row[SCHEMA_COLUMNNAME].ToString();
                    col.Ordinal = int.Parse( row[SCHEMA_ORDINAL].ToString());
                    cols.Add (col);
                }
            }
            return cols;
        }
    }
}
